<script>
    var passwordVerifier = require('wallet-handle').passwordVerify;

    $(function () {

        var form = new Vue({
            el:'#form',
            data:{
                balance:window.balance,
            },
            mounted:function(){
            },
            methods:{
                submit:function(event){
                    if (!validator.form()){
                        return;
                    }

                    var self = this;
                    var that = event.target;
                    var promise = new Promise(function (resolve, reject) {
                        passwordVerifier($('[name=wallet_password]').val(),function(ret,message){
                            if (ret){
                                resolve();
                            }else{
                                alert('钱包密码不正确');
                            }
                        });
                    }).then(function (){
                        if ($('[name=amount]').val() > this.balance){
                            alert('提现金额不能超过可提余额');
                        }else{
                            axios.post('/ajax/wallet/applycash', {
                                amount:$('[name=amount]').val()
                            }).then(function (response) {
                                    if (response.data.code == 200) {
                                        alert('您提现申请已提交，等待平台审核放款处理');
                                        location.href = '/wallet';
                                    } else {
                                        alert(response.data.message || '申请提现失败');
                                    }
                                },function (response) {
                                    alert(response.data.message || '申请提现失败');
                                }).catch(function (err) {
                                alert('申请提现失败');
                            });
                        }
                    });

                },
            }
        });

        var validator = $("#form").validate({
            rules: {
                amount:{
                    required:true,
                    number:true,
                    min:window.cash_minimum,
                },
                wallet_password:{
                    required:true,
                }
            },
            messages: {
                amount:{
                    required:'请输入提现金额',
                },
                wallet_password:{
                    required:'请输入钱包密码',
                },
            },
        });
    });
</script>